var animation
var interval
var n = 0
Page({
  width: 0, // 窗口宽度
  data: {
    choose: 0
  },

  onLoad: function () {
    // 获取系统信息
    wx.getSystemInfo({
      success: res => {
        // 获取窗口宽
        this.width = res.windowWidth
      }
    })
  },
  onReady: function () {
    var width = this.width
    var ctx = wx.createCanvasContext('myCanvas')
    ctx.setStrokeStyle('#1c86e5')
    ctx.setLineWidth(15)
    ctx.moveTo(width / 2, 0)
    ctx.lineTo(width / 2, 100)
    ctx.moveTo(width / 2 - 50, 100)
    ctx.lineTo(width / 2 + 50, 100)
    ctx.stroke()
    ctx.draw()
  },
  onUnload() {
    this.n = 0
    clearInterval(this.interval);//停止
  },
  // 创建动画
  onShow: function () {
    animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease'
    })
    this.animation = animation
  },
  rotate: function () {
    n = n + 1
    if (n >= 1000) {
      n = 0
      this.animation.rotate(0, 0).step({ duration: 0 })
    } else {
      this.animation.rotate(120 * n).step()
    }
    this.setData({
      animation: this.animation.export()
    })
  },
  switch_0: function () {
    if (this.data.choose != 0) {
      n = 0
      clearInterval(this.interval);//停止
      animation.rotate(0, 0).step({ duration: 0 })
      this.setData({
        choose: 0,
        animation: animation.export()
      })
    }
  },
  switch_1: function () {
    if (this.data.choose != 1) {
      this.setData({
        choose: 1
      })
      clearInterval(this.interval);//停止
      this.interval = setInterval(this.rotate, 250); //启动,func不能使用括号
    }
  },
  switch_2: function () {
    if (this.data.choose != 2) {
      this.setData({
        choose: 2
      })
      clearInterval(this.interval);//停止
      this.interval = setInterval(this.rotate, 140); //启动,func不能使用括号
    }
  },
  switch_3: function () {
    if (this.data.choose != 3) {
      this.setData({
        choose: 3
      })
      clearInterval(this.interval);//停止
      this.interval = setInterval(this.rotate, 35); //启动,func不能使用括号
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '送给你一个风扇，别客气热了就开哈~',
      // path: '/pages/air-conditioner/air-conditioner'
    }
  }
})